#include "queueList.h"
#include <stdlib.h>
#include "logger.h"
int IsEmpty(Queue Q)
{
    return Q->front == NULL;
}
int IsFull(Queue Q)
{
    return 0;
}
Queue CreateQueue()
{
    Queue Q = malloc(sizeof(*Q));
    Q->front = Q->rear = NULL;
}

void DisposeQueue(Queue Q)
{
    free(Q);
}

void Enqueue(ElementType X, Queue Q)
{
    Position Tmp = malloc(sizeof(*Tmp));
    if (!Tmp)
        FatalError("Out of space!");
    Tmp->Next = NULL;
    Tmp->Element = X;
    if (IsEmpty(Q))
    {
        Q->rear = Q->front = Tmp;
    }
    else
    {
        Q->rear->Next = Tmp;
        Q->rear = Tmp;
    }
}

ElementType Front(Queue Q)
{
    if (IsEmpty(Q))
        FatalError("Q is empty");
    return Q->front->Element;
}

void Dequeue(Queue Q)
{
    if (IsEmpty(Q))
        FatalError("Q is empty");
    Position Tmp = Q->front;
    Q->front = Q->front->Next;
    //如果只有一个元素，那么队尾的指针也会受到影响
    if (Q->front == NULL)
        Q->rear = NULL;
    free(Tmp);
}

ElementType FrontAndDequeue(Queue Q)
{
    if (IsEmpty(Q))
        FatalError("Q is empty");
    Position Tmp = Q->front;
    ElementType element = Tmp->Element;
    Q->front = Q->front->Next;
    //如果只有一个元素，那么队尾的指针也会受到影响
    if (Q->front == NULL)
        Q->rear = NULL;
    free(Tmp);
    return element;
}